package com.komorebishao.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.komorebishao.crm.dao.CrmCustomerMapper;
import com.komorebishao.crm.dto.CustomerQuery;
import com.komorebishao.crm.pojo.CrmCustomer;
import com.komorebishao.crm.pojo.CrmCustomerExample;
import com.komorebishao.crm.pojo.CrmDict;
import com.komorebishao.crm.service.CustomerService;
import com.komorebishao.crm.service.DictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: KomorebiShao
 * @Description:
 * @Date: Created in 2020-07-03
 * @Modified By:
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    private List<CrmDict> fromType;
    private List<CrmDict> industryType;
    private List<CrmDict> levelType;


    @Autowired
    private CrmCustomerMapper mapper;
    @Autowired
    private DictService dictService;

    public PageInfo<CrmCustomer> selectCustomerList(CustomerQuery query) {
        //根据查询对象，进行条件查询
        CrmCustomerExample crmCustomerExample = new CrmCustomerExample();
        CrmCustomerExample.Criteria criteria = crmCustomerExample.createCriteria();

        if (StringUtils.isNotBlank(query.getCustName())){
            criteria.andCustNameLike("%"+query.getCustName()+"%");
        }
        if (StringUtils.isNotBlank(query.getCustIndustry())){
            criteria.andCustIndustryEqualTo(query.getCustIndustry());
        }
        if (StringUtils.isNotBlank(query.getCustSource())){
            criteria.andCustSourceEqualTo(query.getCustSource());
        }
        if (StringUtils.isNotBlank(query.getCustLevel())){
            criteria.andCustLevelEqualTo(query.getCustLevel());
        }

        //启用分页插件
        PageHelper.startPage(query.getPage(),query.getSize());
        List<CrmCustomer> crmCustomers = mapper.selectByExample(crmCustomerExample);
        //通过分页插件去获取分页信息,其中包括了查询到的结果以及分页相关数据
        PageInfo<CrmCustomer> crmCustomerPageInfo = new PageInfo<CrmCustomer>(crmCustomers);
        //将id转为名称
        stranslateDictToName(crmCustomers);
        return crmCustomerPageInfo;
    }

    public CrmCustomer selectById(Integer id) {
        CrmCustomer crmCustomer = mapper.selectByPrimaryKey(Long.valueOf(id));

        return crmCustomer;
    }

    public boolean updateCustomer(CrmCustomer crmCustomer) {
        return mapper.updateByPrimaryKey(crmCustomer) > 0;
    }

    public boolean deleteCustomerById(Long id) {
        return mapper.deleteByPrimaryKey(id) > 0;
    }

    //仅在第一次查询数据库时使用
    private void stranslateDictToName(List<CrmCustomer> crmCustomers) {
        if (levelType == null){
            System.out.println("初始化信息service");
            //查询需要的 来源、行业、级别
            fromType = dictService.selectDictByTypeCode("002");
            industryType = dictService.selectDictByTypeCode("001");
            levelType = dictService.selectDictByTypeCode("006");
        }
        for (CrmCustomer crmCustomer : crmCustomers){
            for (CrmDict dict : levelType){
                if (dict.getDictId().equals(crmCustomer.getCustLevel())){
                    crmCustomer.setCustLevel(dict.getDictItemName());
                }
            }
            for (CrmDict dict : fromType){
                if (dict.getDictId().equals(crmCustomer.getCustSource())){
                    crmCustomer.setCustSource(dict.getDictItemName());
                }
            }
            for (CrmDict dict : industryType){
                if (dict.getDictId().equals(crmCustomer.getCustIndustry())){
                    crmCustomer.setCustIndustry(dict.getDictItemName());
                }
            }
        }
    }
}
